package com.project.myxima.presenters;

import com.project.myxima.data.HistoryDao;
import com.project.myxima.interfaces.IHistoryPresenter;
import com.project.myxima.interfaces.IHistoryViewCallback;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class HistoryPresenter implements IHistoryPresenter , IHistoryViewCallback {

    HistoryDao mHistoryDao = HistoryDao.getInstance();
    private List<IHistoryViewCallback> mViewCallbacks = new ArrayList<>();

    //================================================================================
    //Single Instance
    private HistoryPresenter(){
        mHistoryDao.setCallback(this);
    }

    private static class InnerHolder{
        private static final HistoryPresenter INSTANCE = new HistoryPresenter();
    }

    public static HistoryPresenter getInstance(){
        return InnerHolder.INSTANCE;
    }


    //===================================================
    //presenter for history
    @Override
    public void deleteHistory(Track track) {
        mHistoryDao.delHistory(track);
    }

    @Override
    public void addHistory(Track track) {
        mHistoryDao.addHistory(track);
    }

    @Override
    public void cleanHistory() {
        mHistoryDao.clearHistory();
    }

    @Override
    public void getHistoryList() {
        mHistoryDao.getHistoryList();
    }

    @Override
    public void registerViewCallback(IHistoryViewCallback iHistoryViewCallback) {
        if (iHistoryViewCallback != null
                &&!mViewCallbacks.contains(iHistoryViewCallback)) {
            mViewCallbacks.add(iHistoryViewCallback);
        }
    }

    @Override
    public void unregisterViewCallback(IHistoryViewCallback iHistoryViewCallback) {
        mViewCallbacks.remove(iHistoryViewCallback);
    }


    //=================================================================
    //callback for history
    @Override
    public void onAddHistory(boolean isSuccess) {
        for(IHistoryViewCallback callback:mViewCallbacks){
            callback.onAddHistory(isSuccess);
        }
    }

    @Override
    public void onDelHistory(boolean isSuccess) {
        for(IHistoryViewCallback callback:mViewCallbacks){
            callback.onDelHistory(isSuccess);
        }
    }

    @Override
    public void onCleanHistory(boolean isSuccess) {
        for(IHistoryViewCallback callback:mViewCallbacks){
            callback.onCleanHistory(isSuccess);
        }
    }

    @Override
    public void onHistoryListLoad(List<Track> trackList) {
        for(IHistoryViewCallback callback:mViewCallbacks){
            callback.onHistoryListLoad(trackList);
        }
    }

    @Override
    public void onHistoryListLoadError() {
        for(IHistoryViewCallback callback:mViewCallbacks){
            callback.onHistoryListLoadError();
        }
    }



}
